module Shrimp {
    export class EventManager {
        events: Object = {};

        on(eventName: string, callback: Function): void {
            if (eventName in this.events === false) {
                this.events[eventName] = [];
            }

            (<any[]>this.events[eventName]).push(callback);
        }

        trigger(eventName: string, ...args: any[]): void {
            if (eventName.split(".").length === 1) {
                for (var event in this.events) {
                    if (event.split(".")[0] === eventName.split(".")[0]) {
                        for (var i = 0; i < (<Function[]>this.events[event]).length; i++) {
                            (<Function[]>this.events[event])[i].apply(this, args);
                        }
                    }
                }
            } else {
                for (var i = 0; i < (<Function[]>this.events[eventName]).length; i++) {
                    (<Function[]>this.events[eventName])[i].apply(this, args);
                }
            }
        }

        off(eventName?: string, handler?: Function): void {
            if (eventName in this.events) {
                delete this.events[eventName];
            }
        }
    }
}